/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.main;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import model.game.GameController;
import model.game.InformationLayer;

/**
 *
 * @author dolphinigle
 */
public class CellListener extends MouseAdapter {

  private int x_, y_;
  private InformationLayer info_;
  private Information output_;
  private GameController controller_;
  private MainView owner_;

  public CellListener(InformationLayer infolayer, Information infooutput,
          GameController controller, MainView owner, 
          int x, int y) {
    controller_ = controller;
    owner_ = owner;
    info_ = infolayer;
    output_ = infooutput;
    x_ = x;
    y_ = y;
  }

  @Override
  public void mouseEntered(MouseEvent e) {
    if (info_.containsCreatureAt(x_, y_)) {

      String leveltext = "";
      if (info_.getCreatureLevelAt(x_, y_) >= 0) {
        leveltext = "<br/>Lv: " + info_.getCreatureLevelAt(x_, y_);
      }

      String hptext = "";
      if (info_.getCreatureMaxHPAt(x_, y_) >= 0) {
        hptext =
        "<br/>" +
              "HP: " +
              info_.getCreatureCurrentHPAt(x_, y_) +
                            "/" +
              info_.getCreatureMaxHPAt(x_, y_);
      }
      
      output_.displayInformation(
              
              "Name: " +
              info_.getCreatureNameAt(x_, y_) +
              leveltext +
              hptext +
              "<br/>" +
              "Description:<br/>" +
              info_.getCreatureDescriptionAt(x_, y_));
    } else {
      output_.setVisible(false);
    }
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    if (info_.canUpgradeCreatureAt(x_, y_)) {
            int answer = JOptionPane.showConfirmDialog(owner_,
                    "Do you want to upgrade " +
                    info_.getUpgradableCreatureNameAt(x_, y_) +
                    " Lv." +
                    info_.getUpgradableCreatureLevelAt(x_, y_) +
                    " by paying " +
                    info_.getUpgradableCreaturePriceAt(x_, y_) +
                    "?",
                    "Upgrade",
                    JOptionPane.YES_NO_OPTION);
            if (answer == JOptionPane.YES_OPTION) {
              controller_.upgradeCreatureAt(x_, y_);
              owner_.updateAll();
            }

          } else {
          }
  }

}
